﻿using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.SceneManagement;

namespace Elvex
{
    public class ScenesManager : MonoBehaviour
    {
        private Dictionary<string, SceneRequest> m_sceneRquests { get; } = new Dictionary<string, SceneRequest>();

        internal bool AddSceneRequest(SceneRequest req)
        {
            if (null == req)
            {
                Debug.LogError("AddSceneRequest null.");
                return false;
            }

            if (m_sceneRquests.ContainsKey(req.Name))
            {
                Debug.LogError("m_sceneRquests ContainsKey.");
                return false;
            }

            m_sceneRquests.Add(req.Name, req);
            return true;
        }

        private void LoadSceneCompleted(Scene scene, LoadSceneMode mode)
        {
            Debug.Log("LoadSceneCompleted");
            if (m_sceneRquests.TryGetValue(scene.name, out var req))
            {
                req.OnSceneLoaded(scene, mode);
                m_sceneRquests.Remove(scene.name);
            }
            else
            {
                Debug.LogError("ScenesManager Request Not Found.");
            }
        }

        private void Awake()
        {
            SceneManager.sceneLoaded += LoadSceneCompleted;
        }
    }
}



